package com.siqi.hr.spring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public final class SEApplicationContext {
	private static ApplicationContext context = null;

	public static ApplicationContext getSEApplicationContext() {
		if (context == null) {
			context = new ClassPathXmlApplicationContext(new String[] {"config/spring-beans.xml"});
		}
		return context;
	}

	public static ApplicationContext getSEApplicationContext(String cfg) {
		if (context == null) {
			context = new ClassPathXmlApplicationContext(cfg);
		}
		return context;
	}

	public static ApplicationContext getSEApplicationContext(String[] cfgs) {
		if (context == null) {
			context = new ClassPathXmlApplicationContext(cfgs);
		}
		return context;
	}

	public static <T> T getBean(String name, Class<T> requiredType) {
		context = getSEApplicationContext();
		return context.getBean(name, requiredType);
	}

	@SuppressWarnings("unchecked")
	public static <T> T getBean(String name) {
		context = getSEApplicationContext();
		return (T) context.getBean(name);
	}

	public static <T> T getBean(String cfg, String name, Class<T> requiredType) {
		context = getSEApplicationContext(cfg);
		return context.getBean(name, requiredType);
	}

	public static <T> T getBean(String[] cfgs, String name,Class<T> requiredType) {
		context = getSEApplicationContext(cfgs);
		return context.getBean(name, requiredType);
	}
}
